package 跳跃游戏;

public class Solution {

    public boolean canJump(int startIndex , int[] nums) {
        int j = nums.length - startIndex  - 1;
        if(nums[startIndex] >= j){
            return true;
        }
        else if(nums[startIndex] == 0){
            return false;
        }
        else{
            int max = 0;
            for (int i = 1; i <= nums[startIndex] ; i++) {
                int currentHeight = nums[startIndex + i ];
                int juli = currentHeight - nums[startIndex] + i;
                if(max < juli){
                    max = juli;
                }
            }
            nums[startIndex + nums[startIndex]] = max;
            return canJump(startIndex + nums[startIndex],nums);
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = new int[]{2,3,1,1,4};
//        int[] nums = new int[]{3,2,1,0,4};
//        int[] nums = new int[]{1,0,0,4};
//        int[] nums = new int[]{3,0,8,2,0,0,1};
//        int[] nums = new int[]{3,0,8,2,0,0,1};
        System.out.println(solution.canJump(nums));
    }

    public boolean canJump(int[] nums) {
        return canJump(0,nums);
    }

}
